package com.dropgame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.dropgame.main.*;

public abstract class TransitionScreen implements Screen
{
   static final float FADE_DURATION = 0.2f;
   
   public DropGame game;
   SpriteBatch batcher;
   OrthographicCamera guiCam;
   Vector3 touchPoint;
   
   float fadeTime = 0;
   float fadeAlpha = 0;
   
   Screen nextScreen;
   boolean goingToNextScreen = false;
   boolean enteringScreen = false;

   public TransitionScreen(DropGame game)
   {
      this.game = game;
      
      guiCam = new OrthographicCamera(320, 480);
      guiCam.position.set(320 / 2, 480 / 2, 0);
      
      touchPoint = new Vector3();
      batcher= new SpriteBatch();
      
      fadeTime = 0;
      enteringScreen = true;
      create();
      nextScreen = null;
      Gdx.app.log("The tran screen constructor has been called ", "");
     
   }

   /**
    * Call this method to change screen using a transition.
    */
   public void nextScreen(Screen screen)
   {
      fadeTime = 0;
      goingToNextScreen = true;
      nextScreen = screen;
   }

   /**
    * Screen initialization goes here.
    */
   public abstract void create();
   
   /**
    * Screen rendering goes here.
    */
   public abstract void renderScreen(float delta);

   @Override
   public void render(float delta)
   {
      if (delta > 0.1f)
         delta = 0.1f;
      Gdx.app.log("The render has been called ", "");
      
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);
      
      renderScreen(delta);
      
      // fadein when entering the screen
      if (enteringScreen) {
    	 Gdx.app.log("The transition screen is enteringscreen ", "");
         fadeTime += delta;
         
         if (fadeTime == 0) {
            fadeAlpha = 1;
         }
         else {
            fadeAlpha = 1-(1/FADE_DURATION)*fadeTime;
            
            if (fadeAlpha < 0) fadeAlpha = 0;
            if (fadeAlpha > 1) fadeAlpha = 1;
         }
         
         if (fadeTime >= FADE_DURATION) {
            
            fadeTime = FADE_DURATION;
            enteringScreen = false;
         }
         
         batcher.begin();
         batcher.enableBlending();
         AssetLoader.transitionSprite.setBounds(guiCam.position.x-(320 / 2), guiCam.position.y-(480 / 2),
                 320, 480);
         AssetLoader.transitionSprite.draw(batcher, fadeAlpha);
         batcher.end();
      }
      
      // fadeout when leaving
      if (goingToNextScreen) {
    	 Gdx.app.log("The tran screen is exitingscreen ", "");
         fadeTime += delta;
         
         if (fadeTime == 0) {
            fadeAlpha = 0;
         }
         else {
            fadeAlpha = (1/FADE_DURATION)*fadeTime;
            
            if (fadeAlpha < 0) fadeAlpha = 0;
            if (fadeAlpha > 1) fadeAlpha = 1;
         }
         
         if (fadeTime >= FADE_DURATION) {
            
            fadeTime = FADE_DURATION;
            game.setScreen(nextScreen);
            goingToNextScreen = false;
         }
         
         batcher.begin();
         batcher.enableBlending();
         AssetLoader.transitionSprite.setBounds(guiCam.position.x-(320 / 2), guiCam.position.y-(480 / 2),
                     320, 480);
         AssetLoader.transitionSprite.draw(batcher, fadeAlpha);
         
         batcher.end();
      }
   }

   @Override
   public abstract void resize(int width, int height);

   @Override
   public abstract void show();

   @Override
   public abstract void hide();

   @Override
   public abstract void pause();

   @Override
   public abstract void resume();

   @Override
   public abstract void dispose();
   
}
